/*
 * Copyright (c) 2009, Jan Pastor. All Rights Reserved.
 */

package sk.jp.jitag.dao.db4o.model;

import java.util.List;

import sk.jp.jitag.dao.db4o.Column;
import sk.jp.jitag.dao.db4o.CollectionsFactory;
import sk.jp.jitag.dao.db4o.BaseEntity;

public class Person extends BaseEntity {

    public static final int NAME_MAXLEN = 32;
    public static final int SURNAME_MAXLEN = 64;

    private static final Column _NAME = new Column("name");
    private static final Column _SURNAME = new Column("surname");
    private static final Column _AGE = new Column("age");
    private static final Column _ADDRESS = new Column("address");
    private static final Column _SEX = new Column("sex");
    private static final Column _PARENTS = new Column("parents");

    private String name;
    private String surname;
    private Integer age;
    private Address address = null;
    private Sex sex;
    private List<Person> parents = null;

    //----------------------------------------------------------------------

    public Person(String name, String surname, Integer age, Sex sex) {
        checkNotNullNotEmpty(_NAME, name);
        checkMaxLen(_NAME, name, NAME_MAXLEN);
        checkNotNullNotEmpty(_SURNAME, surname);
        checkMaxLen(_SURNAME, surname, SURNAME_MAXLEN);
        checkNotNull(_SEX, sex);

        this.name = name;
        this.surname = surname;
        this.age = age;
        this.sex = sex;
    }

    public String getName() {
        return name;
    }

    public String getSurname() {
        return surname;
    }

    public Integer getAge() {
        return age;
    }

    /**
     * @return address or null.
     */
    public Address getAddress() {
        return address;
    }

    public Person setAddress(Address address) {
        this.address = address;
        return this;
    }

    public Sex getSex() {
        return sex;
    }

    /**
     * @return list of parents or null.
     */
    public List<Person> getParents() {
        return parents;
    }

    public Person setParents(List<Person> parents) {
        this.parents = CollectionsFactory.createList(parents);
        return this;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder();
        sb.append("Person");
        sb.append("{name='").append(name).append('\'');
        sb.append(", surname='").append(surname).append('\'');
        sb.append(", age=").append(age);
        sb.append(", address=").append(address);
        sb.append(", sex=").append(sex);
        sb.append(", parents.size=").append(parents != null ? parents.size() : "null");
        sb.append('}');
        return sb.toString();
    }
}
